package Model;


import java.sql.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;

import Entity.*;
import S2.server.ConnectionManage;


public class SaleModel {

	private static Connection conn = ConnectionManage.getInstance()
			.getConnection();

	public static Sale addSale(String Id,String name, Date startDate, Date endDate, Product product) throws SQLException {
		
		String sql = "INSERT INTO products (id,name,description,price,picture)"+"VALUES(?,?,?,?,?)";
		ResultSet rs = null;
		Sale bean =  new Sale();

		try (PreparedStatement stmt = conn.prepareStatement(sql);) {
			stmt.setString(1, Id);
			stmt.setString(2, name);
			stmt.setDate(3, (java.sql.Date) startDate);
			stmt.setDate(4, (java.sql.Date) endDate);
			stmt.setObject(5, product);
			
			rs = stmt.executeQuery();

			rs = stmt.executeQuery();
			if (rs.next()) {
				bean.setName(name);
				bean.setId(Id);
				bean.setStartDate(startDate);
				bean.setEndDate(endDate);
				bean.setProducts(product);
			} else {
				return null;
			}

		} catch (SQLException e) {
			System.err.println(e);
			return null;
		} finally {
			if (rs != null) {
				rs.close();
			}
		}
		return bean;
		

	}
	public static Sale UpdateSale(String Id,String name,Date startDate,Date endDate, Product product) throws SQLException {
		String sql = "Update INTO Sale (id,name,statDate,endDate,product)"+"VALUES(?,?,?,?,?)";
		ResultSet rs = null;
		Sale bean =  new Sale();

		try (PreparedStatement stmt = conn.prepareStatement(sql);) {
			stmt.setString(1, name);
			stmt.setDate(2, (java.sql.Date) startDate);
			stmt.setDate(3, (java.sql.Date) endDate);
			stmt.setObject(4,(Product)product);
			stmt.setString(1, Id);
			stmt.setString(2, name);
			stmt.setDate(3, (java.sql.Date) startDate);
			stmt.setDate(4, (java.sql.Date) endDate);
			stmt.setObject(5, product);
			
			rs = stmt.executeQuery();
			
			if (rs.next()) {
				bean.setName(name);
				bean.setId(Id);
				bean.setStartDate(startDate);
				bean.setEndDate(endDate);
				bean.setProducts(product);
				return bean;
			} else {
				return null;
			}
	}
	 catch (SQLException e) {
		System.err.println(e);
		return null;
	} finally {
		if (rs != null) {
			rs.close();
		}
	}
	
		
	}
	public static Sale geteSale(String Id,String name, Product product) throws SQLException {
		
		String sql = "SELECT * FROM" + " Sale WHERE" + " name=? AND Id=? ";
		ResultSet rs = null;
		Sale bean =  new Sale();

		try (PreparedStatement stmt = conn.prepareStatement(sql);) {
			stmt.setString(1, name);
			stmt.setString(2, Id);

			rs = stmt.executeQuery();
			
			if (rs.next()) {
				bean.setId(Id);
				bean.setName(name);
				bean.setStartDate(rs.getDate("startDate"));
				bean.setEndDate(rs.getDate("endDate"));
				bean.setProducts((Product)product);
				

				bean.setStartDate(rs.getDate("startDate"));
				bean.setEndDate(rs.getDate("endtDate"));
				bean.setProducts((Product)(rs.getObject("product")));

				return bean;
			} else {
				return null;
			}

		} catch (SQLException e) {
			System.err.println(e);
			return null;
		} finally {
			if (rs != null) {
				rs.close();
			}
		}
}
}
